//
//  HHHomeViewController.swift
//  HHWeibo
//
//  Created by hehuafeng on 2017/2/14.
//  Copyright © 2017年 雷厉峰行. All rights reserved.
//

import UIKit

// MARK: - 定义重用标识
private let cellID = "home"

class HHHomeViewController: HHBaseViewController {
    
    /// 首页数据
    lazy var listViewModel = HHStatusListViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 2. 加载数据
        loadData()
        
    }
    
    
    /// 加载数据
    override func loadData() {
        
        listViewModel.loadStatus(pullup: isPullup) { (isSuccess, shoudleRefersh) in
            // 1. 结束刷新控件
            self.refershControl?.endRefreshing()
            self.isPullup = false
            
            
            if shoudleRefersh {
                // 2. 刷新表格
                self.tableView?.reloadData()
            }
        }
    }
    
    override func setupTableView() {
        // 0. 调用父类方法
        super.setupTableView()
        
        // 1. 设置导航栏左上角
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange, target: self, action: #selector(showFriends))
        
        // MARK: - 注册cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
    }
    
    /// MARK: 监听方法
    @objc private func showFriends() {
        let vc = HHDemoController()
        
        vc.title = "测试"
        navigationController?.pushViewController(vc, animated: true)
    }

}

// MARK: - 数据源、代理方法
extension HHHomeViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
        
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        
        return cell
    }
}
